﻿using IoTSocket.Core;
using JunionRobotTaskManager.Models.ElevatorControl;
using System.ComponentModel;

namespace JunionRobotTaskManager.Models.ElevatorControl
{
    public class Step
    {
        public ControlStream Invoker { get; init; }

        public int Number { get; init; }

        public Command Command { get; init; }

        public bool IsComplete { get; private set; }

        public Step(int number, Command command, ControlStream invoker)
        {
            Number = number;
            Command = command;
            Invoker = invoker;
        }

        public async Task<bool> ExecuteAsync()
        {
            switch (Command)
            {
                case Command.EnterElevator:
                    {
                        IsComplete = await Invoker.Belonger.CallAndOpenAsync(Invoker.SourceFloor);
                        return IsComplete;
                    }
                case Command.CloseDoor:
                    {
                        IsComplete = await Invoker.Belonger.CloseDoorAsync();
                        return IsComplete;
                    }
                case Command.LeaveElevator:
                    {
                        if (Invoker.Belonger.CurrentFloor == Invoker.TargetFloor)
                        {
                            IsComplete = await Invoker.Belonger.OpenDoorAsync();
                        }
                        else
                        {
                            IsComplete = await Invoker.Belonger.CallAndOpenAsync(Invoker.TargetFloor);
                        }
                        return IsComplete;
                    }
                default: return false;
            }
        }
    }

    public enum Command
    {
        [Description("请求进入电梯")]
        EnterElevator,
        [Description("请求关门")]
        CloseDoor,
        [Description("请求离开电梯")]
        LeaveElevator,
    }
}
